package com.sesame.mq.rabbit.direct.config;

import lombok.extern.apachecommons.CommonsLog;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


/**
 * direct 类型的行为是"先匹配, 再投送".
 * 即在绑定时设定一个 routing_key, 消息的routing_key 匹配时, 才会被交换器投送到绑定的队列中去.
 */
@CommonsLog
@Component
public class RabbitDirectConfig {

    public static final String USER_QUEUE_1 = "directQueue1";
    public static final String USER_QUEUE_2 = "directQueue2";
    public static final String USER_QUEUE_3 = "directQueue3";
    //直连路由键名
    public static final String directRouteKey = "direct.userKey";
    //直连交换机
    public static final String exchange = "directUserExchange";

    //队列1
    @Bean
    public Queue directQueue1() {
    /* 是否持久化
     仅创建者可以使用的私有队列，断开后自动删除
     当所有消费客户端连接断开后，是否自动删除队列*/
        return new Queue(USER_QUEUE_1, true, false, false);
    }
    //队列2
    @Bean
    public Queue directQueue2() {
        return new Queue(USER_QUEUE_2, true, false, false);
    }
    @Bean
    public Queue directQueue3() {
        return new Queue(USER_QUEUE_3, true, false, false);
    }


    //定义直连交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(exchange, true, false);
    }
    //将队列1绑定到直接交换机
    @Bean
    public Binding bindDirectQueue1() {
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with(directRouteKey);
    }
    //将队列2绑定到直接交换机
    @Bean
    public Binding bindDirectQueue2() {
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with(directRouteKey);
    }
    @Bean
    public Binding bindDirectQueue3() {
        return BindingBuilder.bind(directQueue3()).to(directExchange()).with("adasdfsadf");
    }

}
